package com.yao.interceptor;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.yao.entity.Permit;
import com.yao.service.PermitService;

@Component
public class PermitInterceptor implements HandlerInterceptor {
	@Autowired
	private PermitService permitService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("exec-PermitInterceptor:8002");
		HttpSession session = request.getSession();
		String name = (String) session.getAttribute("name");
		List<Permit> permitList = permitService.getPermit(name);
		List<String> collect = permitList.stream().map((o) -> o.getUrl()).collect(Collectors.toList());
		for (String string : collect) {
			System.out.println(string + ":" + request.getRequestURI());
			System.out.println();
		}
		if (!collect.contains(request.getRequestURI())) {
			System.out.println("收到非法访问");
			throw new RuntimeException("收到非法访问");
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
